package com.suredy.app.porject.ctrl.flow;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suredy.app.porject.constants.ContractType;
import com.suredy.app.porject.constants.FlowStatus;
import com.suredy.app.porject.ctrl.view.ViewRoleCtrl;
import com.suredy.app.porject.model.Bid;
import com.suredy.app.porject.model.BidAgency;
import com.suredy.app.porject.model.Biddinger;
import com.suredy.app.porject.model.Contract;
import com.suredy.app.porject.service.BidAgencySrv;
import com.suredy.app.porject.service.BidSrv;
import com.suredy.app.porject.service.BiddingerSrv;
import com.suredy.app.porject.service.ContractSrv;
import com.suredy.app.porject.service.FlowOfBid2UserSrv;
import com.suredy.app.porject.service.FlowOfContract2UserSrv;
import com.suredy.app.porject.service.ProjectConstants;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.security.entity.OrgEntity;

/**
 * 获取业务数据
 * 
 * @author VIVID.G
 * @since 2015-12-26
 * @version v0.1
 */
@Controller
@RequestMapping("/flow/business-data")
public class BusinessDataFromFlowCtrl extends BaseCtrl {

	@Autowired
	private BiddingerSrv biddingerSrv;

	@Autowired
	private BidAgencySrv bidAgencySrv;

	@Autowired
	private ProjectConstants constants;

	@Autowired
	private BidSrv bidSrv;

	@Autowired
	private ContractSrv contractSrv;

	@Autowired
	private FlowOfBid2UserSrv bid2UserSrv;

	@Autowired
	private FlowOfContract2UserSrv contract2UserSrv;

	@RequestMapping(value = "/todo-bid")
	public String myTodoBid(Bid search, Model model) {
		String view = "app/flow/business-data/todo-bid";

		// biddinger.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBiddingerInfo() != null && search.getBiddingerInfo().getBiddinger() != null) {
			if ("null".equalsIgnoreCase(search.getBiddingerInfo().getBiddinger().getId()))
				search.getBiddingerInfo().setBiddinger(null);
		}

		DetachedCriteria dc = this.bidSrv.getDc(search);

		String[] userIds = new String[] {this.getUser().getId()};

		List<Bid> data = this.bid2UserSrv.getAllBidOf(dc, userIds, true, FlowStatus.FS1000_BID_CREATED);

		model.addAttribute("data", data);
		model.addAttribute("bidWays", this.constants.getBidWays(false));
		model.addAttribute("search", search);

		return view;
	}

	@RequestMapping(value = "/completed-bid")
	public String myCompletedBid(Bid search, Model model) {
		String view = "app/flow/business-data/completed-bid";

		FlowStatus[] fs = new FlowStatus[] {FlowStatus.FS1001_BID_CONTRACT_CREATED, FlowStatus.FS1002_BID_COMPLETED};

		// biddinger.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBiddingerInfo() != null && search.getBiddingerInfo().getBiddinger() != null) {
			if ("null".equalsIgnoreCase(search.getBiddingerInfo().getBiddinger().getId()))
				search.getBiddingerInfo().setBiddinger(null);
		}

		DetachedCriteria dc = this.bidSrv.getDc(search);

		// 送交合同专责和完成合同签订都属于已办完
		dc.add(Restrictions.in("flowStatus", fs));

		String[] userIds = new String[] {this.getUser().getId()};

		List<Bid> data = this.bid2UserSrv.getAllBidOf(dc, userIds, null);

		model.addAttribute("biddingers", this.getBiddingers());
		model.addAttribute("bidTypes", this.constants.getBidTypes(false));
		model.addAttribute("bidWays", this.constants.getBidWays(false));
		model.addAttribute("search", search);
		model.addAttribute("data", data);

		return view;
	}

	@RequestMapping(value = "/all-bid")
	public String allBid(Bid search, Model model) {
		String view = "app/flow/business-data/all-bid";

		// biddinger.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBiddingerInfo() != null && search.getBiddingerInfo().getBiddinger() != null) {
			if ("null".equalsIgnoreCase(search.getBiddingerInfo().getBiddinger().getId()))
				search.getBiddingerInfo().setBiddinger(null);
		}

		DetachedCriteria dc = this.bidSrv.getDc(search);

		List<Bid> data = this.bid2UserSrv.getAllBidOf(dc, null, null);

		model.addAttribute("biddingers", this.getBiddingers());
		model.addAttribute("bidTypes", this.constants.getBidTypes(false));
		model.addAttribute("bidWays", this.constants.getBidWays(false));
		model.addAttribute("search", search);
		model.addAttribute("data", data);

		return view;
	}

	@RequestMapping(value = "/win-bid-for-contract")
	public String myWinBidForContract(Bid search, Model model) {
		String view = "app/flow/business-data/win-bid-for-contract";

		// biddinger.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBiddingerInfo() != null && search.getBiddingerInfo().getBiddinger() != null) {
			if ("null".equalsIgnoreCase(search.getBiddingerInfo().getBiddinger().getId()))
				search.getBiddingerInfo().setBiddinger(null);
		}

		DetachedCriteria dc = this.bidSrv.getDc(search);

		String[] userIds = new String[] {this.getUser().getId()};

		List<Bid> data = this.bid2UserSrv.getAllBidOf(dc, userIds, true, FlowStatus.FS1001_BID_CONTRACT_CREATED);

		model.addAttribute("bidWays", this.constants.getBidWays(false));
		model.addAttribute("search", search);
		model.addAttribute("data", data);

		return view;
	}

	@RequestMapping(value = "/todo-contract")
	public String myTodoContract(Contract search, Model model) {
		String view = "app/flow/business-data/todo-contract";

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));

		String[] userIds = new String[] {this.getUser().getId()};

		List<Contract> data = this.contract2UserSrv.getAllContractOf(dc, userIds, true, FlowStatus.FS2000_CONTRACT_TO_CREATE);

		model.addAttribute("data", data);
		model.addAttribute("search", search);

		return view;
	}

	@RequestMapping(value = "/completed-contract")
	public String myCompletedContract(Contract search, Model model) {
		String view = "app/flow/business-data/completed-contract";

		FlowStatus[] fs = new FlowStatus[] {FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED};

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));
		dc.add(Restrictions.in("flowStatus", fs));

		String[] userIds = new String[] {this.getUser().getId()};

		List<Contract> data = this.contract2UserSrv.getAllContractOf(dc, userIds, null);

		model.addAttribute("data", data);
		model.addAttribute("search", search);

		return view;
	}

	@RequestMapping(value = "/wait-contract")
	public String myWaitContract(Contract search, Model model) {
		String view = "app/flow/business-data/wait-contract";

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));

		List<Contract> data = this.contract2UserSrv.getAllContractOf(dc, null, true, FlowStatus.FS2001_CONTRACT_CREATED);

		model.addAttribute("data", data);
		model.addAttribute("search", search);

		return view;
	}

	@RequestMapping(value = "/started-contract")
	public String myStartedContract(Contract search, Model model) {
		String view = "app/flow/business-data/started-contract";

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));

		String[] userIds = new String[] {this.getUser().getId()};

		List<Contract> data = this.contract2UserSrv.getAllContractOf(dc, userIds, true, FlowStatus.FS2002_CONTRACT_STARTED);

		model.addAttribute("data", data);
		model.addAttribute("search", search);

		return view;
	}

	@RequestMapping(value = "/has-in-hand-contract")
	public String hasInMyhand(Contract search, Model model) {
		String view = "app/flow/business-data/has-in-hand-contract";

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));

		String[] userIds = new String[] {this.getUser().getId()};

		List<Contract> data = this.contract2UserSrv.getAllContractOf(dc, userIds, null);

		model.addAttribute("data", data);
		model.addAttribute("search", search);

		return view;
	}

	@RequestMapping(value = "/all-contract")
	public String allContract(Contract search, Model model) {
		String view = "app/flow/business-data/all-contract";

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));

		List<Contract> data = this.contract2UserSrv.getAllContractOf(dc, null, null, FlowStatus.FS2001_CONTRACT_CREATED);

		model.addAttribute("data", data);
		model.addAttribute("search", search);
		model.addAttribute("roles", new ViewRoleCtrl(this.getRoles(), this.getUser()));

		return view;
	}

	@RequestMapping(value = "/sjb-contract")
	public String sjbAllContract(Contract search, Integer page, Integer pageSize, Model model) {
		String view = "app/flow/business-data/all-contract-for-scbld";

		if (page == null || page < 1)
			page = 1;

		if (pageSize == null || pageSize < 1)
			pageSize = 30;

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));

		List<Contract> data = this.contract2UserSrv.getContractOf(dc, null, null, page, pageSize, FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED);

		model.addAttribute("data", data);
		model.addAttribute("search", search);
		model.addAttribute("roles", new ViewRoleCtrl(this.getRoles(), this.getUser()));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contract2UserSrv.getContractCountOf(dc, null, null, FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED));

		return view;
	}

	private List<Biddinger> getBiddingers() {
		Biddinger search = new Biddinger();
		search.setOwner(new OrgEntity());
		search.getOwner().setId(this.getUser().getOrgId());

		List<Biddinger> data = this.biddingerSrv.readByEntity(search);

		return data == null || data.isEmpty() ? null : data;
	}

	private List<BidAgency> getBidAgencys() {
		BidAgency search = new BidAgency();
		search.setOwner(new OrgEntity());
		search.getOwner().setId(this.getUser().getOrgId());

		List<BidAgency> data = this.bidAgencySrv.readByEntity(search);

		return data == null || data.isEmpty() ? null : data;
	}

}
